<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-09
 * Time: 15:53
 */

namespace app\api\controller;


use app\common\model\Student;
use app\common\model\UserStudent;

class Bind extends Base
{
    public $userStudentModel;

    public function __construct()
    {
        parent::__construct();
        $this->userStudentModel = new UserStudent();
    }


    /**
     * 绑定队员
     * @return \think\response\Json
     */
    public function bind()
    {
        if (request()->isPost()) {
            $data = input('');
            if ($data['idcard']) {
                $student = Student::getOne(['idcard' => ['like', "%{$data['idcard']}"]]);
                if ($student) {
                    $bind_data = [
                        'uid' => $this->uid,
                        'sid' => $student['id']
                    ];
                    $bind = UserStudent::getOne($bind_data);
                    if ($bind) {
                        return returnJson(202, '已绑定该队员，请勿重复绑定');
                    } else {
                        $bind_data['update_time'] = time();
                        $bind_data['create_time'] = time();
                        $res = UserStudent::create($bind_data);
                        if ($res) {
                            if($this->member['grade_id']==1){
                                $save_data = [
                                    'grade_id' => 5,
                                    'update_time' => time()
                                ];
                                \app\common\model\User::update($save_data,['id'=>$this->uid]);
                            }

                            $where = [
                                'a.uid' => $this->uid
                            ];
                            $list = $this->userStudentModel
                                ->alias('a')
                                ->join('zkhb_student b', 'a.sid=b.id', 'left')
                                ->where($where)
                                ->field('b.id,b.name')
                                ->select();
                            if ($list) {
                                $list = $list->toArray();
                            }
                            return returnJson(200, '绑定成功', $list);
                        }
                    }
                }
            }
        }
        return returnJson(202, '绑定失败');
    }


    /**
     * 队员绑定列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function bind_list()
    {
        if (request()->isPost()) {
            $data = input('');
            $where = [
                'a.uid' => $this->uid
            ];
            $list = $this->userStudentModel
                ->alias('a')
                ->join('zkhb_student b', 'a.sid=b.id', 'left')
                ->where($where)
                ->field('b.id,b.name')
                ->select();
            if ($list) {
                $list = $list->toArray();
            }
            return returnJson(200, '',$list);
        }
    }

}